/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entidad.Empleado;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author user
 */
public class EmpleadoDAO {
    
    private static EntityManagerFactory emf=Persistence.createEntityManagerFactory("CRMPU");



    public boolean crear(Empleado object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean salida=false;
        try {
            em.persist(object);
            em.getTransaction().commit();
            salida = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return salida;
        }
    }

     public boolean eliminar(Empleado object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Empleado emp = em.find(Empleado.class, object.getID());
        boolean ret = false;
        try {
            em.remove(emp);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }

    public Empleado leer(Empleado par) {

        EntityManager em = emf.createEntityManager();
        Empleado empleado = null;
        Query q = em.createQuery("SELECT e from Empleado e " +
                "WHERE c.ID LIKE :ID")
                .setParameter("ID", par.getID());
        try {
            empleado = (Empleado) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            empleado = (Empleado) q.getResultList().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return empleado;
        }
    }

  
    public boolean actualizar(Empleado object, Empleado nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            object = leer(object);
            object.setID(nuevoObjeto.getID());
            object.setNombre(nuevoObjeto.getNombre());
            object.setRol(nuevoObjeto.getRol());
            object.setContraseña(nuevoObjeto.getContraseña());
            object.setTarea(nuevoObjeto.getTarea());
           
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }
    
}
